package com.tidc.seaserverservice.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.tidc.seaservermapper.dao.RoleDao;
import com.tidc.seaservermapper.dao.UserDao;
import com.tidc.seaservermodel.entity.Fres;
import com.tidc.seaservermodel.entity.User;
import com.tidc.seaservermodel.exception.TidcBadParent;
import com.tidc.seaservermodel.exception.TidcRestException;
import com.tidc.seaservermodel.properties.RoleProperties;
import com.tidc.seaservermodel.config.RedisConfig;
import com.tidc.seaservermodel.util.RedisUtil;
import com.tidc.seaserverservice.service.UserService;
import com.tidc.seaserverservice.util.SendCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author ：冯涛滔
 * @date ：Created in 2020/10/12 21:16
 * @description：
 * @modified By：
 * @version:
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private UserDao userDao;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private RoleDao roleDao;
    @Resource
    private RoleProperties roleProperties;
    @Resource
    private SendCodeUtil sendCodeUtil;

    @Override
    public Fres<String> register(User user) throws TidcBadParent {
        Fres<String> fres = null;
        if (redisUtil.checkCode(RedisConfig.REGISTER_CODE + user.getPhone(),user.getCode())) {
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            User check = userDao.queryByPhone(user.getPhone());
            if (check == null) {
                user.setTypeClassify(1);
                userDao.insert(user);
//                角色绑定
                roleDao.grantedToUserRole(user.getId(), roleProperties.getSUPERVISOR_USER_ROLE_IDS());
                fres = Fres.Success();
            } else {
                fres = Fres.badRequestParent("", "手机号已被注册");
            }
        } else {
            fres = Fres.badRequestParent("验证码错误", "验证码错误");
        }
        return fres;
    }

    @Override
    public Fres<String> getCode(int type,String phone, HttpServletRequest req) throws TidcBadParent, TidcRestException, ClientException {
        Fres<String> fres = null;
        int code = (int) ((Math.random() * 9 + 1) * 100000);
        int flag = redisUtil.checkIp(req);
        if (flag != 200) {
            throw new TidcBadParent("此ip多次反复请求24小时不允许访问如果再次访问禁止时间继续延长");
        }
        String key = null;
        switch (type){
            case 1:
                key = RedisConfig.REGISTER_CODE;
                break;
            case 2:
                key = RedisConfig.ADD_SECONDARY_CODE;
                break;
            case 3:
                key = RedisConfig.PUT_SECONDARY_CODE;
                break;
        }
        sendCodeUtil.sendMessage(phone, code);
        redisUtil.set(key + phone, code + "", 2 * 60 * 1000);
        fres = Fres.Success("短信已发送", "短信已发送");
        log.info("验证码已发送 phone {} code {}", phone, code);
        return fres;
    }

}
